<?php
namespace Admin\Controller;
use Think\Controller;
class FinanceController extends AuthController {
    public function index(){
    		$month = I('month');
    		if($month == ''){
		    	$month = date('Y-m');
    		}
// 	    	$order = M("Order");
// 	    	$where['code'] = array('like',$month."%");
// 	    	$list = $order->where($where)->select();
//     		$this->assign('list',$list);
			$user = M("User");
			$users = $user->select();
			$this->assign('users',$users);
    		$sql_where = "o.cdate LIKE '{$month}%'";
    		$uid = I("uid");
    		if($uid){
    			$sql_where.="AND o.uid = '{$uid}'";
    		}
    		$food = M ()->query ( "SELECT od.unit,od.category,od.food_name as label, SUM(od.num) as data,SUM(od.yprice*od.num) as yprice,SUM(od.price*od.num) as price FROM vw_order_detail od,vw_order o  WHERE od.oid = o.id AND {$sql_where} GROUP BY od.cid  ORDER BY od.id ASC" );
    		$money = M ()->query ( "SELECT o.cdate,SUM(o.food_yprice) as yprice,SUM(o.food_price) as price FROM vw_order o  WHERE {$sql_where} GROUP BY o.cdate  ORDER BY o.cdate ASC" );
			$this->assign('food',$food);
			$this->assign('money',$money);
			$pics = array();
			foreach ($food as $item){
				$pic = array();
				$pic['unit']  = $item['unit'];
				$pic['label'] = "{$item['label']}({$item['category']})";
				$pic['data'] = $item['data'];
				$pics[] = $pic;
			}
			$this->assign('pics',$pics);
    		$total = M ()->query ( "SELECT o.status,o.payment,o.food_price as price,o.food_yprice as yprice FROM vw_order o  WHERE {$sql_where} ORDER BY o.cdate ASC" );
			foreach($total as $k=>$v){
				$zj['yprice'] +=$v['yprice'];
				$zj['price'] +=$v['price'];
				if($v['payment'] == 1){
					$zj['receipt'] += $v['price'];
				}
				switch ($v['status']){
					case 0:
						$zj['undist'] += $v['price'];
						$zj['unreceipt'] += $v['price'];
						break;
					case 1:
						$zj['disting'] += $v['price'];
						$zj['unreceipt'] += $v['price'];
						break;
					case 2:
						$zj['unreceipt'] += $v['price'];
						break;
					case 3:
						$zj['receipt'] += $v['price'];
						break;
				}
			}
			$zj['profit'] =$zj['price'] -$zj['yprice'];
			$this->assign('zj',$zj);
			$this->assign('month',$month);
			$this->assign('uid',$uid);
    		$this->display();
    }
}